// 参考官方github购物车stroe写法
// initial state
import axios from 'axios'

const state = {
  userInfo: {
    token: '',
    user: {}
  }
}

// getters
const getters = {}

// actions
const actions = {
  login ({ commit }, data) {
    return axios.post('/accounts/login', data).then(res => {
      if (res.status === 400) return
      commit('setUserInfo', res.data)
      console.log(res.data)
      // 👇这里相当于外部调用时候promise的调用
      Promise.resolve()
    }).catch(err => {
      console.log(err)
    })
  }
}

// mutations
const mutations = {
  setUserInfo (state, data) {
    state.userInfo = data
    localStorage.setItem('userInfo', JSON.stringify(data))
  },
  clearUserInfo (state, data) {
    state.userInfo = {
      token: '',
      user: {}
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
